//
//  HMNetworkTool.swift
//  iOSWeibo11
//
//  Created by Apple on 16/7/19.
//  Copyright © 2016年 Apple. All rights reserved.
//

// #import <UIKit/UIKit.h>
import UIKit

import AFNetworking

// WebService -> XML


/// 网络工具类,直接继承AFN
class HMNetworkTool: NSObject {

    let afnManager: AFHTTPSessionManager
    
    // 网络请求前面一段不会变化
    // https://api.weibo.com/2/statuses/friends_timeline.json
    // https://api.weibo.com/2/statuses/mentions/ids.json
    // https://api.weibo.com/2/favorites.json
    // 单例
    // 遍历构造函数没办法在子类里面重写
    static let sharedNetworkTool: HMNetworkTool = HMNetworkTool()
    
    // 自己来实现构造函数
    override init() {
        afnManager = AFHTTPSessionManager(baseURL: NSURL(string: "https://api.weibo.com/"))
        // 添加返回序列化器支持的类型
        afnManager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        super.init()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func requestGET(URLString: String, parameters: AnyObject?, progress: ((NSProgress) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {
        
//        bfnManager.NBGET(URLString)
        afnManager.GET(URLString, parameters: parameters, progress: progress, success: success, failure: failure)
    }
    
    // 不带上传的
    func requestPOST(URLString: String, parameters: AnyObject?, progress: ((NSProgress) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {
        
//        bfnManager.NBPOST(URLString)
        afnManager.POST(URLString, parameters: parameters, progress: progress, success: success, failure: failure)
    }
    
    // 带上传功能
    func requestPOST(URLString: String, parameters: AnyObject?, constructingBodyWithBlock: ((AFMultipartFormData) -> Void)?, progress: ((NSProgress) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {
        
//        bfnManager.NBPOST(URLString)
        afnManager.POST(URLString, parameters: parameters, constructingBodyWithBlock: constructingBodyWithBlock, progress: progress, success: success, failure: failure)
    }
    
}
